home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / Alacarte / util.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.2 KB  |  275 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import gtk
  6. import gmenu
  7. from ConfigParser import ConfigParser
  8.  
  9. class DesktopParser(ConfigParser):
  10.     
  11.     def __init__(self, filename = None, file_type = 'Application'):
  12.         ConfigParser.__init__(self)
  13.         self.filename = filename
  14.         self.file_type = file_type
  15.         if filename:
  16.             if len(self.read(filename)) == 0:
  17.                 self.add_section('Desktop Entry')
  18.             
  19.         else:
  20.             self.add_section('Desktop Entry')
  21.         self._list_separator = ';'
  22.  
  23.     
  24.     def optionxform(self, option):
  25.         return option
  26.  
  27.     
  28.     def get(self, option, locale = None):
  29.         locale_option = option + '[%s]' % locale
  30.         
  31.         try:
  32.             value = ConfigParser.get(self, 'Desktop Entry', locale_option)
  33.         except:
  34.             
  35.             try:
  36.                 value = ConfigParser.get(self, 'Desktop Entry', option)
  37.             return None
  38.  
  39.  
  40.         if self._list_separator in value:
  41.             value = value.split(self._list_separator)
  42.         
  43.         if value == 'true':
  44.             value = True
  45.         
  46.         if value == 'false':
  47.             value = False
  48.         
  49.         return value
  50.  
  51.     
  52.     def set(self, option, value, locale = None):
  53.         if locale:
  54.             option = option + '[%s]' % locale
  55.         
  56.         if value == True:
  57.             value = 'true'
  58.         
  59.         if value == False:
  60.             value = 'false'
  61.         
  62.         if isinstance(value, tuple) or isinstance(value, list):
  63.             value = self._list_separator.join(value) + ';'
  64.         
  65.         ConfigParser.set(self, 'Desktop Entry', option, value)
  66.  
  67.     
  68.     def write(self, file_object):
  69.         file_object.write('[Desktop Entry]\n')
  70.         items = []
  71.         if not self.filename:
  72.             file_object.write('Encoding=UTF-8\n')
  73.             file_object.write('Type=' + str(self.file_type) + '\n')
  74.         
  75.         for item in self.items('Desktop Entry'):
  76.             items.append(item)
  77.         
  78.         items.sort()
  79.         for item in items:
  80.             file_object.write(item[0] + '=' + item[1] + '\n')
  81.         
  82.  
  83.  
  84.  
  85. def getUniqueFileId(name, extension):
  86.     append = 0
  87.     while append == 0:
  88.         filename = name + extension
  89.     filename = name + '-' + str(append) + extension
  90.     if extension == '.desktop':
  91.         path = getUserItemPath()
  92.         if not os.path.isfile(os.path.join(path, filename)) and not getItemPath(filename):
  93.             break
  94.         
  95.     elif extension == '.directory':
  96.         path = getUserDirectoryPath()
  97.         if not os.path.isfile(os.path.join(path, filename)) and not getDirectoryPath(filename):
  98.             break
  99.         
  100.     
  101.     append += 1
  102.     continue
  103.     return filename
  104.  
  105.  
  106. def getUniqueRedoFile(filepath):
  107.     append = 0
  108.     while None:
  109.         new_filepath = filepath + '.redo-' + str(append)
  110.         if not os.path.isfile(new_filepath):
  111.             break
  112.             continue
  113.         append += 1
  114.         continue
  115.         return new_filepath
  116.  
  117.  
  118. def getUniqueUndoFile(filepath):
  119.     (filename, extension) = os.path.split(filepath)[1].rsplit('.', 1)
  120.     append = 0
  121.     while extension == 'desktop':
  122.         path = getUserItemPath()
  123.     if extension == 'directory':
  124.         path = getUserDirectoryPath()
  125.     elif extension == 'menu':
  126.         path = getUserMenuPath()
  127.     
  128.     new_filepath = os.path.join(path, filename + '.' + extension + '.undo-' + str(append))
  129.     if not os.path.isfile(new_filepath):
  130.         break
  131.         continue
  132.     append += 1
  133.     continue
  134.     return new_filepath
  135.  
  136.  
  137. def getUserMenuPath():
  138.     menu_dir = None
  139.     if os.environ.has_key('XDG_CONFIG_HOME'):
  140.         menu_dir = os.path.join(os.environ['XDG_CONFIG_HOME'], 'menus')
  141.     else:
  142.         menu_dir = os.path.join(os.environ['HOME'], '.config', 'menus')
  143.     if os.path.isfile(os.path.split(menu_dir)[0]):
  144.         os.rename(os.path.split(menu_dir)[0], os.path.split(menu_dir)[0] + '.old')
  145.     
  146.     if not os.path.isdir(menu_dir):
  147.         os.makedirs(menu_dir)
  148.     
  149.     return menu_dir
  150.  
  151.  
  152. def getItemPath(file_id):
  153.     if os.environ.has_key('XDG_DATA_DIRS'):
  154.         for system_path in os.environ['XDG_DATA_DIRS'].split(':'):
  155.             file_path = os.path.join(system_path, 'applications', file_id)
  156.             if os.path.isfile(file_path):
  157.                 return file_path
  158.         
  159.     
  160.     file_path = os.path.join('/', 'usr', 'share', 'applications', file_id)
  161.     if os.path.isfile(file_path):
  162.         return file_path
  163.     return False
  164.  
  165.  
  166. def getUserItemPath():
  167.     item_dir = None
  168.     if os.environ.has_key('XDG_DATA_HOME'):
  169.         item_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'applications')
  170.     else:
  171.         item_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'applications')
  172.     if not os.path.isdir(item_dir):
  173.         os.makedirs(item_dir)
  174.     
  175.     return item_dir
  176.  
  177.  
  178. def getDirectoryPath(file_id):
  179.     home = getUserDirectoryPath()
  180.     file_path = os.path.join(home, file_id)
  181.     if os.path.isfile(file_path):
  182.         return file_path
  183.     file_path = os.path.join('/', 'usr', 'share', 'desktop-directories', file_id)
  184.     if os.path.isfile(file_path):
  185.         return file_path
  186.     return False
  187.  
  188.  
  189. def getUserDirectoryPath():
  190.     menu_dir = None
  191.     if os.environ.has_key('XDG_DATA_HOME'):
  192.         menu_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'desktop-directories')
  193.     else:
  194.         menu_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'desktop-directories')
  195.     if not os.path.isdir(menu_dir):
  196.         os.makedirs(menu_dir)
  197.     
  198.     return menu_dir
  199.  
  200.  
  201. def getSystemMenuPath(file_name):
  202.     if os.environ.has_key('XDG_CONFIG_DIRS'):
  203.         for system_path in os.environ['XDG_CONFIG_DIRS'].split(':'):
  204.             file_path = os.path.join(system_path, 'menus', file_name)
  205.             if os.path.isfile(file_path):
  206.                 return file_path
  207.         
  208.     
  209.     file_path = os.path.join('/', 'etc', 'xdg', 'menus', file_name)
  210.     if os.path.isfile(file_path):
  211.         return file_path
  212.     return False
  213.  
  214.  
  215. def getUserMenuXml(tree):
  216.     system_file = getSystemMenuPath(tree.get_menu_file())
  217.     name = tree.root.get_menu_id()
  218.     menu_xml = "<!DOCTYPE Menu PUBLIC '-//freedesktop//DTD Menu 1.0//EN' 'http://standards.freedesktop.org/menu-spec/menu-1.0.dtd'>\n"
  219.     menu_xml += '<Menu>\n  <Name>' + name + '</Name>\n  '
  220.     menu_xml += '<MergeFile type="parent">' + system_file + '</MergeFile>\n</Menu>\n'
  221.     return menu_xml
  222.  
  223.  
  224. def getIcon(item, for_properties = False):
  225.     (pixbuf, path) = (None, None)
  226.     if item == None:
  227.         if for_properties:
  228.             return (None, None)
  229.         return None
  230.     if isinstance(item, str):
  231.         iconName = item
  232.     else:
  233.         iconName = item.get_icon()
  234.     if iconName and '/' not in iconName and iconName[-3:] in ('png', 'svg', 'xpm'):
  235.         iconName = iconName[:-4]
  236.     
  237.     icon_theme = gtk.icon_theme_get_default()
  238.     
  239.     try:
  240.         pixbuf = icon_theme.load_icon(iconName, 24, 0)
  241.         path = icon_theme.lookup_icon(iconName, 24, 0).get_filename()
  242.     except:
  243.         if iconName and '/' in iconName:
  244.             
  245.             try:
  246.                 pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(iconName, 24, 24)
  247.                 path = iconName
  248.  
  249.         
  250.         if pixbuf == None:
  251.             if for_properties:
  252.                 return (None, None)
  253.             if item.get_type() == gmenu.TYPE_DIRECTORY:
  254.                 iconName = 'gnome-fs-directory'
  255.             elif item.get_type() == gmenu.TYPE_ENTRY:
  256.                 iconName = 'application-default-icon'
  257.             
  258.             
  259.             try:
  260.                 pixbuf = icon_theme.load_icon(iconName, 24, 0)
  261.                 path = icon_theme.lookup_icon(iconName, 24, 0).get_filename()
  262.             return None
  263.  
  264.         
  265.  
  266.     if pixbuf == None:
  267.         return None
  268.     if pixbuf.get_width() != 24 or pixbuf.get_height() != 24:
  269.         pixbuf = pixbuf.scale_simple(24, 24, gtk.gdk.INTERP_HYPER)
  270.     
  271.     if for_properties:
  272.         return (pixbuf, path)
  273.     return pixbuf
  274.  
  275.